package edu.fzu.crud.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import edu.fzu.crud.bean.Student;
import edu.fzu.crud.bean.vo.StuInfoVo;
import edu.fzu.crud.common.Result;
import edu.fzu.crud.dao.SchoolMapper;
import edu.fzu.crud.dao.StudentMapper;
import edu.fzu.crud.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author JohnCarraway
 * @create 2020-12-27 20:44
 */
@Controller
@RequestMapping("/stu")
public class StudentController {

    @Autowired
    StudentService studentService;

    @Autowired
    StudentMapper studentMapper;

    @Autowired
    SchoolMapper schoolMapper;

    @RequestMapping(value = "/query")
    @ResponseBody
    public Result getAllStuList(){
        List<Student> studentList = studentService.queryAllStudentInfo();
        return Result.success().add("stuList", studentList);
    }

    @RequestMapping(value = "/getAllStudentInfo")
    public String queryAllStuInfo(Model model){
        List<Student> studentList = studentService.queryAllStudentInfo();
        model.addAttribute("stuList", studentList);
        return "student";
    }

    @RequestMapping(value = "/sendMessageToAngular")
    @ResponseBody
    public Result sendMessage(){
        return Result.success().add("message", "王珊珊");
    }

    @RequestMapping("/queryAll")
    public String getStuList(@RequestParam(value = "pn", defaultValue = "1") Integer pn,
                          Model model){

        PageHelper.startPage(pn, 5);
        List<Student> studentList = studentService.queryAllStudentInfo();

        PageInfo<Student> pageInfo = new PageInfo<>(studentList, 5);
        model.addAttribute("stuList", pageInfo);

        return "student";
    }

    @RequestMapping(value = "/queryStuInfo")
    @ResponseBody
    public Result queryStuInfo(@RequestParam(value = "pn", defaultValue = "1") Integer pn){
        PageHelper.startPage(pn, 5);
        List<Student> studentList = studentService.queryAllStudentInfo();

        PageInfo<Student> pageInfo = new PageInfo<>(studentList, 5);
        return Result.success().add("newStuList", pageInfo);
    }

    @RequestMapping(value = "addStuInfoByExcel", method = RequestMethod.POST)
    @ResponseBody
    public Result addStuInfoByExcel(HttpServletResponse response, StuInfoVo stuInfoVo){
        boolean isSuccess = studentService.saveStuInfo(response, stuInfoVo);
        if (isSuccess) return Result.success();
        else return Result.failure();
    }

    @RequestMapping(value = "exportStuInfoToExcel", method = RequestMethod.POST)
    @ResponseBody
    public Result exportStuInfoToExcel(@RequestBody List<StuInfoVo> stuInfoVoList){
        boolean isSuccess = studentService.exportStuInfoToExcel(stuInfoVoList);
        if (isSuccess) return Result.success();
        else return Result.failure();
    }

    @RequestMapping(value = "saveStuInfo", method = RequestMethod.POST)
    @ResponseBody
    public Result readAndSaveStuInfoByExcel(MultipartFile file){

        studentService.readAndSaveStuInfoFromExcel(file, studentMapper, schoolMapper);
        return Result.success();
    }
}
